{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "3f8e1bd4",
   "metadata": {},
   "source": [
    "---  \n",
    "\n",
    "* 2022-08-30\n",
    "* python学习第二周  \n",
    "* 记录人：weiting  \n",
    "\n",
    "---  \n",
    "# 本周学习内容  \n",
    "## 1.书本p4  \n",
    "## 2.1模块--标准库  \n",
    "* 标准库是已经写好的一些模块，可以直接使用：import+模块名称  \n",
    "* 例：  \n",
    "sys模块  \n",
    "os模块  \n",
    "datetime模块\n",
    "\n",
    "import--拿  \n",
    "import+模块  \n",
    "## 2.2模块--开源模块（pypi.org)  \n",
    "## 2.3自定义模块  \n",
    "## 3.赋值  \n",
    "## 4.变量  \n",
    "* 变量命名"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e7d96942",
   "metadata": {},
   "source": [
    "_1.书本p4_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "084fb938",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "This minute seems a little odd.\n"
     ]
    }
   ],
   "source": [
    "from datetime import datetime\n",
    "\n",
    "odds = [  1,  3,  5,  7,  9, 11,13,15,17,19,\n",
    "              21,23,25,27,29,31,33,35,37,39,\n",
    "              41,43,45,47,49,51,53,55,57,59 ]\n",
    "\n",
    "right_this_minute = datetime.today().minute\n",
    "\n",
    "if right_this_minute in odds:\n",
    "    print(\"This minute seems a little odd.\")\n",
    "else:\n",
    "    print(\"Not an odd minute.\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "e8b81d1d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "807609\n",
      "Not an odd minute.\n",
      "809717\n"
     ]
    }
   ],
   "source": [
    "import datetime\n",
    "# 本项目用到了时间模块\n",
    "print(datetime.datetime.today().microsecond)\n",
    "\n",
    "odds = [  1, 3,  5,  7,  9, 11,13,15,17,19,\n",
    "             21,23,25,27,29,31,33,35,37,39,\n",
    "             41,43,45,47,49,51,53,55,57,59]\n",
    "\n",
    "right_this_minute = datetime.datetime.today().minute\n",
    "\n",
    "if right_this_minute in odds:\n",
    "    print(\"This minute seems a little odd.\")\n",
    "else:\n",
    "    print(\"Not an odd minute.\")\n",
    "\n",
    "print(datetime.datetime.today().microsecond)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f2e6fa6b",
   "metadata": {},
   "source": [
    "_2.模块_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "569d5696",
   "metadata": {},
   "outputs": [],
   "source": [
    "import datetime"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "2a3c899f",
   "metadata": {},
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "module 'datetime' has no attribute 'today'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "Input \u001b[1;32mIn [3]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43mdatetime\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtoday\u001b[49m\n",
      "\u001b[1;31mAttributeError\u001b[0m: module 'datetime' has no attribute 'today'"
     ]
    }
   ],
   "source": [
    "datetime.today"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "e42a255e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.time"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "datetime.time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "1422d8c8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.time(0, 0)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "datetime.time()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "61c280bd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.date(2022, 8, 30)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "datetime.date.today()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "dc36656f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "30"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "datetime.datetime.today().day"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "83755a05",
   "metadata": {},
   "outputs": [],
   "source": [
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "6ea4ceaf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<function time.localtime>"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "time.localtime"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "e8bba484",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "cannot assign to literal (3645974723.py, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  Input \u001b[1;32mIn [11]\u001b[1;36m\u001b[0m\n\u001b[1;33m    time.localtime().tm_hour=16, tm_min=42,tm_wday=1\u001b[0m\n\u001b[1;37m                             ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m cannot assign to literal\n"
     ]
    }
   ],
   "source": [
    "time.localtime().tm_hour=16, tm_min=42,tm_wday=1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6e37b025",
   "metadata": {},
   "source": [
    "### 测试"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "545e064b",
   "metadata": {},
   "outputs": [],
   "source": [
    "import time "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "54219263",
   "metadata": {},
   "outputs": [],
   "source": [
    "import datetime"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "12b16f93",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.datetime(2022, 8, 31, 22, 27, 46, 739409)"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "datetime.datetime.now()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "24a3aa29",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'2022-08-31 22:35:11'"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "9908bd9b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "time.struct_time(tm_year=2022, tm_mon=8, tm_mday=30, tm_hour=17, tm_min=25, tm_sec=8, tm_wday=1, tm_yday=242, tm_isdst=0)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "time.localtime()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "bc6efadb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "time.localtime().tm_wday"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "48ab5c99",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (3476667018.py, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  Input \u001b[1;32mIn [10]\u001b[1;36m\u001b[0m\n\u001b[1;33m    time.localtime().tm_wday, tm_hour.\u001b[0m\n\u001b[1;37m                                      ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "time.localtime().tm_wday, tm_hour."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "eb780e42",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.datetime(2022, 8, 31, 14, 30)"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "datetime.datetime.strptime(str(datetime.datetime.now().date())+'14:30', '%Y-%m-%d%H:%M')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "3564b512",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<function datetime.strptime>"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "datetime.datetime.strptime"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "fe608698",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.date(2022, 8, 31)"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "datetime.datetime.now().date()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "72e86dff",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.time(23, 0, 8, 456223)"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "datetime.datetime.now().time()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "81b4d23e",
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "time data '23:01:42.61607214:30' does not match format '%Y-%m-%d%H:%M'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "Input \u001b[1;32mIn [47]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43mdatetime\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mdatetime\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mstrptime\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mstr\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mdatetime\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mdatetime\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mnow\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtime\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\u001b[38;5;241;43m+\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43m14:30\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43m%\u001b[39;49m\u001b[38;5;124;43mY-\u001b[39;49m\u001b[38;5;124;43m%\u001b[39;49m\u001b[38;5;124;43mm-\u001b[39;49m\u001b[38;5;132;43;01m%d\u001b[39;49;00m\u001b[38;5;124;43m%\u001b[39;49m\u001b[38;5;124;43mH:\u001b[39;49m\u001b[38;5;124;43m%\u001b[39;49m\u001b[38;5;124;43mM\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m)\u001b[49m\n",
      "File \u001b[1;32mD:\\Anaconda\\lib\\_strptime.py:568\u001b[0m, in \u001b[0;36m_strptime_datetime\u001b[1;34m(cls, data_string, format)\u001b[0m\n\u001b[0;32m    565\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m_strptime_datetime\u001b[39m(\u001b[38;5;28mcls\u001b[39m, data_string, \u001b[38;5;28mformat\u001b[39m\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;132;01m%a\u001b[39;00m\u001b[38;5;124m \u001b[39m\u001b[38;5;124m%\u001b[39m\u001b[38;5;124mb \u001b[39m\u001b[38;5;132;01m%d\u001b[39;00m\u001b[38;5;124m \u001b[39m\u001b[38;5;124m%\u001b[39m\u001b[38;5;124mH:\u001b[39m\u001b[38;5;124m%\u001b[39m\u001b[38;5;124mM:\u001b[39m\u001b[38;5;124m%\u001b[39m\u001b[38;5;124mS \u001b[39m\u001b[38;5;124m%\u001b[39m\u001b[38;5;124mY\u001b[39m\u001b[38;5;124m\"\u001b[39m):\n\u001b[0;32m    566\u001b[0m     \u001b[38;5;124;03m\"\"\"Return a class cls instance based on the input string and the\u001b[39;00m\n\u001b[0;32m    567\u001b[0m \u001b[38;5;124;03m    format string.\"\"\"\u001b[39;00m\n\u001b[1;32m--> 568\u001b[0m     tt, fraction, gmtoff_fraction \u001b[38;5;241m=\u001b[39m \u001b[43m_strptime\u001b[49m\u001b[43m(\u001b[49m\u001b[43mdata_string\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43mformat\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[0;32m    569\u001b[0m     tzname, gmtoff \u001b[38;5;241m=\u001b[39m tt[\u001b[38;5;241m-\u001b[39m\u001b[38;5;241m2\u001b[39m:]\n\u001b[0;32m    570\u001b[0m     args \u001b[38;5;241m=\u001b[39m tt[:\u001b[38;5;241m6\u001b[39m] \u001b[38;5;241m+\u001b[39m (fraction,)\n",
      "File \u001b[1;32mD:\\Anaconda\\lib\\_strptime.py:349\u001b[0m, in \u001b[0;36m_strptime\u001b[1;34m(data_string, format)\u001b[0m\n\u001b[0;32m    347\u001b[0m found \u001b[38;5;241m=\u001b[39m format_regex\u001b[38;5;241m.\u001b[39mmatch(data_string)\n\u001b[0;32m    348\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m found:\n\u001b[1;32m--> 349\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mtime data \u001b[39m\u001b[38;5;132;01m%r\u001b[39;00m\u001b[38;5;124m does not match format \u001b[39m\u001b[38;5;132;01m%r\u001b[39;00m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;241m%\u001b[39m\n\u001b[0;32m    350\u001b[0m                      (data_string, \u001b[38;5;28mformat\u001b[39m))\n\u001b[0;32m    351\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mlen\u001b[39m(data_string) \u001b[38;5;241m!=\u001b[39m found\u001b[38;5;241m.\u001b[39mend():\n\u001b[0;32m    352\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124munconverted data remains: \u001b[39m\u001b[38;5;132;01m%s\u001b[39;00m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;241m%\u001b[39m\n\u001b[0;32m    353\u001b[0m                       data_string[found\u001b[38;5;241m.\u001b[39mend():])\n",
      "\u001b[1;31mValueError\u001b[0m: time data '23:01:42.61607214:30' does not match format '%Y-%m-%d%H:%M'"
     ]
    }
   ],
   "source": [
    "datetime.datetime.strptime(str(datetime.datetime.now().time())+'14:30', '%Y-%m-%d%H:%M')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d7e6b2de",
   "metadata": {},
   "source": [
    "### 练习--判断当前是一周中的星期几，如果是星期二，看下现在几点了，是python课ing么？然后打印Python课表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "35574e5c",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "今天没有python课，嗨嗨嗨\n"
     ]
    }
   ],
   "source": [
    "# 结果\n",
    "import datetime\n",
    "\n",
    "# 星期设定\n",
    "today = time.localtime().tm_wday\n",
    "\n",
    "# 时间范围设定\n",
    "start_time = datetime.datetime.strptime(str(datetime.datetime.now().date())+'14:30', '%Y-%m-%d%H:%M')\n",
    "end_time = datetime.datetime.strptime(str(datetime.datetime.now().date())+'16:50', '%Y-%m-%d%H:%M')\n",
    "\n",
    "# 当前时间\n",
    "now_time = datetime.datetime.now()\n",
    "\n",
    "# 判断条件\n",
    "if today == 2 and now_time > start_time and now_time < end_time:\n",
    "    print(\"python课ing\")\n",
    "else:\n",
    "    print(\"今天没有python课，嗨嗨嗨\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "03998894",
   "metadata": {},
   "source": [
    "### 例子"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "5cedf243",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "今天没python,嗨嗨嗨\n"
     ]
    }
   ],
   "source": [
    "# 例子\n",
    "# tm_wday: 0-6  0是星期一\n",
    "today = time.localtime().tm_wday\n",
    "\n",
    "if today == 1:\n",
    "    print(\"今天要上python课，时间为14：30-16：50\")\n",
    "else:\n",
    "    print(\"今天没python,嗨嗨嗨\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3b27ac03",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
